#include "mainwindow.h"
#include "qdialog.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setFixedSize(1280,720);

    /*绑定所有按钮的信号到槽函数*/
    QList<QPushButton*> buttonList = this->findChildren<QPushButton*>();
    for(int i = 0;i < buttonList.size();i++)
    {
        connect(buttonList[i],SIGNAL(clicked(bool)),this,SLOT(pushbutton_cb()));
    }

    /*获取数据库实例*/
    myDB = MyDB::getInstance();

    /*刷新图表*/
    ui->pushButton_refresh->click();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::pushbutton_cb()
{
    QObject* button = sender();
    if(button == ui->pushButton_refresh)//刷新
    {
        if(ui->pushButton_studentInfo->isChecked())
        {
            delete(myDB->m_model);
            myDB->m_model = NULL;
            myDB->m_model = new QSqlQueryModel;
            myDB->m_model->setQuery("select * from student");
            if(m_sid != "")
            {
                myDB->m_model->setQuery(QString("select * from student where id = \"%1\"").arg(m_sid));
            }
            myDB->m_model->setHeaderData(0, Qt::Horizontal, tr("学号"));
            myDB->m_model->setHeaderData(1, Qt::Horizontal, tr("姓名"));
            myDB->m_model->setHeaderData(2, Qt::Horizontal, tr("性别"));
            myDB->m_model->setHeaderData(3, Qt::Horizontal, tr("年龄"));
            ui->tableView->setModel(myDB->m_model);
        }else if(ui->pushButton_courseInfo->isChecked())
        {
            delete(myDB->m_model);
            myDB->m_model = NULL;
            myDB->m_model = new QSqlQueryModel;
            myDB->m_model->setQuery("select * from course");
            myDB->m_model->setHeaderData(0, Qt::Horizontal, tr("课程号"));
            myDB->m_model->setHeaderData(1, Qt::Horizontal, tr("课程名"));
            myDB->m_model->setHeaderData(2, Qt::Horizontal, tr("学分"));
            ui->tableView->setModel(myDB->m_model);
        }else if(ui->pushButton_gradeInfo->isChecked())
        {
            delete(myDB->m_model);
            myDB->m_model = NULL;
            myDB->m_model = new QSqlQueryModel;
            myDB->m_model->setQuery("select * from grade");
            if(m_sid != "")
            {
                myDB->m_model->setQuery(QString("select * from grade where sid = \"%1\"").arg(m_sid));
            }
            myDB->m_model->setHeaderData(0, Qt::Horizontal, tr("学号"));
            myDB->m_model->setHeaderData(1, Qt::Horizontal, tr("课程号"));
            myDB->m_model->setHeaderData(2, Qt::Horizontal, tr("分数"));
            ui->tableView->setModel(myDB->m_model);
        }
    }else if(button == ui->pushButton_studentInfo)//学生信息
    {
        /*刷新图表*/
        ui->pushButton_refresh->click();
    }else if(button == ui->pushButton_courseInfo)//课程信息
    {
        /*刷新图表*/
        ui->pushButton_refresh->click();
    }else if(button == ui->pushButton_gradeInfo)//分数信息
    {
        /*刷新图表*/
        ui->pushButton_refresh->click();
    }else if(button == ui->pushButton_quit)//退出
    {
        close();
    }else if(button == ui->pushButton_add)//增加
    {
        if(ui->pushButton_studentInfo->isChecked())
        {
            MyDialog* myDialog = new MyDialog();
            myDialog->setFormat(dialogFormat::ADD_STUDENT);
            myDialog->exec();
        }else if(ui->pushButton_courseInfo->isChecked())
        {
            MyDialog* myDialog = new MyDialog();
            myDialog->setFormat(dialogFormat::ADD_COURSE);
            myDialog->exec();
        }else if(ui->pushButton_gradeInfo->isChecked())
        {
            MyDialog* myDialog = new MyDialog();
            myDialog->setFormat(dialogFormat::ADD_GRADE);
            myDialog->exec();
        }
        ui->pushButton_refresh->click();
    }else if(button == ui->pushButton_del)//删除
    {
        if(ui->pushButton_studentInfo->isChecked())
        {
            MyDialog* myDialog = new MyDialog();
            myDialog->setFormat(dialogFormat::DEL_STUDENT);
            myDialog->exec();
        }else if(ui->pushButton_courseInfo->isChecked())
        {
            MyDialog* myDialog = new MyDialog();
            myDialog->setFormat(dialogFormat::DEL_COURSE);
            myDialog->exec();
        }else if(ui->pushButton_gradeInfo->isChecked())
        {
            MyDialog* myDialog = new MyDialog();
            myDialog->setFormat(dialogFormat::DEL_GRADE);
            myDialog->exec();
        }
        ui->pushButton_refresh->click();
    }


}

void MainWindow::showStudentFormat(QString id)
{
    m_sid = id;
    ui->widget_controller->hide();
    ui->pushButton_studentInfo->click();
}
